package ieco.daoimpl;

import ieco.domaine.Choix;

import java.util.List;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@SuppressWarnings("serial")
@Stateless
@LocalBean
public class ChoixDaoImpl extends DaoImpl<String, Choix> {

	@PersistenceContext(unitName="options")
	private EntityManager entityManager;
	
	public ChoixDaoImpl() {
		super(Choix.class);
	}

	public int deleteChoix(int id){
		Query query = entityManager.createQuery("Delete from Choix c where c.option.id = ?1");
		query.setParameter(1, id);
		return query.executeUpdate();
	}
	
	public Choix getChoixByMatriculeEtIdOption(int matricule, int option_id){
		String query = "select c from Choix c where c.etudiant.matricule = ?1 and c.option.id = ?2";
		return find(query, matricule, option_id);
	}
	
	public Choix findChoix(int id) {
		String query = "select c from Choix c where c.id = ?1";
		return find(query, id);
	}
	
	public List<Choix> findAllChoixPourEtudiant(int matricule){
		String query = "select c from Choix c where c.etudiant.matricule = ?1";
		return findAll(query, matricule);
	}
	
	public List<Choix> findAllChoixPourOption(int optionId){
		String query = "select c from Choix c where c.option.id = ?1";
		return findAll(query, optionId);
	}

}
